home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / vm / vm-easymenu.el.z / vm-easymenu.el
Encoding:
Text File  |  1998-05-21  |  7.9 KB  |  224 lines

  1. ;;; easymenu.el --- support the easymenu interface for defining a menu.
  2.  
  3. ;; Copyright (C) 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Keywords: emulations
  6. ;; Author: rms
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; This is compatible with easymenu.el by Per Abrahamsen
  25. ;;; but it is much simpler as it doesn't try to support other Emacs versions.
  26. ;;; The code was mostly derived from lmenu.el.
  27.  
  28. ;;;  Changed 17-May-1995, Kyle Jones
  29. ;;;    Made easy-menu-create-keymaps handle the
  30. ;;;     [ NAME CALLBACK ENABLE ]
  31. ;;;    case properly.  Previously the enabler function was not
  32. ;;;    being put on the property list of the command.
  33. ;;;  Changed 20-May-1995, Kyle Jones
  34. ;;;    Made easy-menu-create-keymaps handle the
  35. ;;;     [ NAME CALLBACK ENABLE SUFFIX ]
  36. ;;;    case properly.
  37. ;;;  Changed 25-May-1995, Kyle Jones
  38. ;;;    Renamed easy-menu- functions to vm-easy-menu- to avoid
  39. ;;;    non-vm compatible versions.
  40. ;;;  Changed 2-July-1995, Kyle Jones
  41. ;;;    If callback is a symbol use it in the menu keymap instead
  42. ;;;    of the uninterned menu-function-XXX symbols.  This allows
  43. ;;;    Emacs' menu code to set this-command properly when
  44. ;;;    launching a command from the menubar.
  45. ;;;
  46. ;;; Code:
  47.  
  48. (provide 'vm-easymenu)
  49.  
  50. (defmacro vm-easy-menu-define (symbol maps doc menu)
  51.   "Define a menu bar submenu in maps MAPS, according to MENU.
  52. The menu keymap is stored in symbol SYMBOL, both as its value
  53. and as its function definition.   DOC is used as the doc string for SYMBOL.
  54.  
  55. The first element of MENU must be a string.  It is the menu bar item name.
  56. The rest of the elements are menu items.
  57.  
  58. A menu item is usually a vector of three elements:  [NAME CALLBACK ENABLE]
  59.  
  60. NAME is a string--the menu item name.
  61.  
  62. CALLBACK is a command to run when the item is chosen,
  63. or a list to evaluate when the item is chosen.
  64.  
  65. ENABLE is an expression; the item is enabled for selection
  66. whenever this expression's value is non-nil.
  67.  
  68. Alternatively, a menu item may have the form: 
  69.  
  70.    [ NAME CALLBACK [ KEYWORD ARG ] ... ]
  71.  
  72. Where KEYWORD is one of the symbol defined below.
  73.  
  74.    :keys KEYS
  75.  
  76. KEYS is a string; a complex keyboard equivalent to this menu item.
  77. This is normally not needed because keyboard equivalents are usually
  78. computed automatically.
  79.  
  80.    :active ENABLE
  81.  
  82. ENABLE is an expression; the item is enabled for selection
  83. whenever this expression's value is non-nil.
  84.  
  85.    :suffix NAME
  86.  
  87. NAME is a string; the name of an argument to CALLBACK.
  88.  
  89.    :style 
  90.    
  91. STYLE is a symbol describing the type of menu item.  The following are
  92. defined:  
  93.  
  94. toggle: A checkbox.  
  95.         Currently just prepend the name with the string \"Toggle \".
  96. radio: A radio button. 
  97. nil: An ordinary menu item.
  98.  
  99.    :selected SELECTED
  100.  
  101. SELECTED is an expression; the checkbox or radio button is selected
  102. whenever this expression's value is non-nil.
  103. Currently just disable radio buttons, no effect on checkboxes.
  104.  
  105. A menu item can be a string.  Then that string appears in the menu as
  106. unselectable text.  A string consisting solely of hyphens is displayed
  107. as a solid horizontal line.
  108.  
  109. A menu item can be a list.  It is treated as a submenu.
  110. The first element should be the submenu name.  That's used as the
  111. menu item in the top-level menu.  The cdr of the submenu list
  112. is a list of menu items, as above."
  113.   (` (progn
  114.        (defvar (, symbol) nil (, doc))
  115.        (vm-easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
  116.  
  117. (defun vm-easy-menu-do-define (symbol maps doc menu)
  118.   ;; We can't do anything that might differ between Emacs dialects in
  119.   ;; `vm-easy-menu-define' in order to make byte compiled files
  120.   ;; compatible.  Therefore everything interesting is done in this
  121.   ;; function. 
  122.   (set symbol (vm-easy-menu-create-keymaps (car menu) (cdr menu)))
  123.   (fset symbol (` (lambda (event) (, doc) (interactive "@e")
  124.             (easy-popup-menu event (, symbol)))))
  125.   (mapcar (function (lambda (map) 
  126.         (define-key map (vector 'menu-bar (intern (car menu)))
  127.           (cons (car menu) (symbol-value symbol)))))
  128.       (if (keymapp maps) (list maps) maps)))
  129.  
  130. (defvar vm-easy-menu-item-count 0)
  131.  
  132. ;; Return a menu keymap corresponding to a Lucid-style menu list
  133. ;; MENU-ITEMS, and with name MENU-NAME.
  134. (defun vm-easy-menu-create-keymaps (menu-name menu-items)
  135.   (let ((menu (make-sparse-keymap menu-name)))
  136.     ;; Process items in reverse order,
  137.     ;; since the define-key loop reverses them again.
  138.     (setq menu-items (reverse menu-items))
  139.     (while menu-items
  140.       (let* ((item (car menu-items))
  141.          (callback (if (vectorp item) (aref item 1)))
  142.          command enabler name)
  143.     (cond ((stringp item)
  144.            (setq command nil)
  145.            (setq name (if (string-match "^-+$" item) "" item)))
  146.           ((consp item)
  147.            (setq command (vm-easy-menu-create-keymaps (car item) (cdr item)))
  148.            (setq name (car item)))
  149.           ((vectorp item)
  150.            (if (symbolp callback)
  151.            (setq command callback)
  152.          (setq command (make-symbol (format "menu-function-%d"
  153.                             vm-easy-menu-item-count)))
  154.          (setq vm-easy-menu-item-count (1+ vm-easy-menu-item-count)))
  155.            (setq name (aref item 0))
  156.            (let ((keyword (aref item 2)))
  157.          (if (and (symbolp keyword)
  158.               (= ?: (aref (symbol-name keyword) 0)))
  159.              (let ((count 2)
  160.                style selected active keys
  161.                arg)
  162.                (while (> (length item) count)
  163.              (setq keyword (aref item count))
  164.              (setq arg (aref item (1+ count)))
  165.              (setq count (+ 2 count))
  166.              (cond ((eq keyword ':keys)
  167.                 (setq keys arg))
  168.                    ((eq keyword ':active)
  169.                 (setq active arg))
  170.                    ((eq keyword ':suffix)
  171.                 (setq name (concat name " " arg)))
  172.                    ((eq keyword ':style)
  173.                 (setq style arg))
  174.                    ((eq keyword ':selected)
  175.                 (setq selected arg))))
  176.                (if keys
  177.                (setq name (concat name "  (" keys ")")))
  178.                (if (eq style 'toggle)
  179.                ;; Simulate checkboxes.
  180.                (setq name (concat "Toggle " name)))
  181.                (if active 
  182.                (put command 'menu-enable active)
  183.              (and (eq style 'radio)
  184.                   selected
  185.                   ;; Simulate radio buttons with menu-enable.
  186.                   (put command 'menu-enable
  187.                    (list 'not selected)))))
  188.            (if (= (length item) 4)
  189.                (setq name (concat name " " (aref item 3))))
  190.            (put command 'menu-enable keyword)))
  191.            (if (keymapp callback)
  192.            (setq name (concat name " ...")))
  193.            (if (symbolp callback)
  194.            nil ;;(fset command callback)
  195.          (fset command (list 'lambda () '(interactive) callback)))))
  196.     (if (null command)
  197.         ;; Handle inactive strings specially--allow any number
  198.         ;; of identical ones.
  199.         (setcdr menu (cons (list nil name) (cdr menu)))
  200.       (if name 
  201.           (define-key menu (vector (intern name)) (cons name command)))))
  202.       (setq menu-items (cdr menu-items)))
  203.     menu))
  204.  
  205. (defun vm-easy-menu-change (path name items)
  206.   "Change menu found at PATH as item NAME to contain ITEMS.
  207. PATH is a list of strings for locating the menu containing NAME in the
  208. menu bar.  ITEMS is a list of menu items, as in `vm-easy-menu-define'.
  209. These items entirely replace the previous items in that map.
  210.  
  211. Call this from `activate-menubar-hook' to implement dynamic menus."
  212.   (let ((map (key-binding (apply 'vector
  213.                  'menu-bar
  214.                  (mapcar 'intern (append path (list name)))))))
  215.     (if (keymapp map)
  216.     (setcdr map (cdr (vm-easy-menu-create-keymaps name items)))
  217.       (error "Malformed menu in `vm-easy-menu-change'"))))
  218.  
  219. (defun vm-easy-menu-remove (menu))
  220.  
  221. (defun vm-easy-menu-add (menu &optional map))
  222.  
  223. ;;; vm-easymenu.el ends here
  224.